//
//  JPEdgeInsetsLabel.m
//  JPKY
//
//  Created by Vitas on 2016/11/15.
//  Copyright © 2016年 Vitas. All rights reserved.
//

#import "JPEdgeInsetsLabel.h"

@implementation JPEdgeInsetsLabel

- (instancetype)init {
    if (self = [super init]) {
        [self setupUI];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

/**
 *  初始化UI
 */
- (void)setupUI {
    self.userInteractionEnabled = YES;
}

- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
    UIEdgeInsets insets = self.edgeInsets;
    CGRect rect = [super textRectForBounds:UIEdgeInsetsInsetRect(bounds, insets)
                    limitedToNumberOfLines:numberOfLines];
    
    rect.origin.x    -= insets.left;
    rect.origin.y    -= insets.top;
    rect.size.width  += (insets.left + insets.right);
    rect.size.height += (insets.top + insets.bottom);
    
    return rect;
}

- (void)drawTextInRect:(CGRect)rect {
    [super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.edgeInsets)];
}

/**
 *  点击的时候从父布局中移除
 */
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    self.isDeleted  = !self.isDeleted;
    if (self.isDeleted) {
        self.textColor = [UIColor redColor];
        if (self.deleteFromSuperView) {
            
            self.deleteFromSuperView(self.text);
        }
    } else {
        self.textColor = globalColor;
    }
}

@end
